(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('waf.test.pti.formatController',[ '$scope', 'uiGridGroupingConstants', 'gridService',
	function($scope, uiGridGroupingConstants, gridService) {
		var ctrl = this;
		ctrl.number = 206.523614;
		ctrl.numberFormat = 'money3';
		ctrl.money = 1253.1234554;
		ctrl.ccy = 'USD';
		ctrl.percentage = 206.523614;
		ctrl.percentageFormat = '#0.0#';
		ctrl.date = new Date();
		ctrl.dateFormat = 'timestampZ';

		var data = [
			{
				"name": "CDA_DYNAPARM3",
				"label": "label.globsystemconfig.dynaparm3",
				"model": "dynamicParams.CDA_DYNAPARM3.value",
				"type": "text",
				"size": 4,
				"readonly": "false",
				"options": {
					
				},
				"dynamicParameter": {
					"desc": "dynamic 3",
					"serviceCode": "CDA",
					"group2": "label.group.globalsystemconfig.dynamic",
					"group3": null,
					"siteCode": "*",
					"name": "CDA_DYNAPARM3",
					"id": "697",
					"dispOrder": "3",
					"javaType": "java.lang.String",
					"dispKey": "label.globsystemconfig.dynaparm3",
					"subCategory": "*",
					"category": "GLOBSYSTEMCONFIG"
				},
				"uppercase": true
			},
			{
				"name": "CDA_DYNAPARM1",
				"label": "label.globsystemconfig.dynaparm1",
				"model": "dynamicParams.CDA_DYNAPARM1.value",
				"type": "radio",
				"size": 4,
				"readonly": "false",
				"options": {
					"data": [{
						"value": "Y",
						"label": "yes"
					},
					{
						"value": "N",
						"label": "no"
					}]
				},
				"dynamicParameter": {
					"desc": "dynamic 1",
					"serviceCode": "CDA",
					"group2": "label.group.globalsystemconfig.dynamic",
					"group3": null,
					"siteCode": "*",
					"name": "CDA_DYNAPARM1",
					"id": "698",
					"dispOrder": "1",
					"javaType": "java.lang.String",
					"dispKey": "label.globsystemconfig.dynaparm1",
					"subCategory": "*",
					"category": "GLOBSYSTEMCONFIG"
				},
				"uppercase": true
			},
			{
				"name": "CDA_DYNAPARM2",
				"label": "label.globsystemconfig.dynaparm2",
				"model": "dynamicParams.CDA_DYNAPARM2.value",
				"type": "select",
				"size": 4,
				"readonly": "false",
				"options": {
					"data": [{
						"value": "option1",
						"label": "yes"
					},
					{
						"value": "option2",
						"label": "no"
					}],
					"hasEmptyOption": true
				},
				"dynamicParameter": {
					"desc": "dynamic 2",
					"serviceCode": "CDA",
					"group2": "label.group.globalsystemconfig.dynamic",
					"group3": null,
					"siteCode": "*",
					"name": "CDA_DYNAPARM2",
					"id": "699",
					"dispOrder": "2",
					"javaType": "java.lang.String",
					"dispKey": "label.globsystemconfig.dynaparm2",
					"subCategory": "*",
					"category": "GLOBSYSTEMCONFIG"
				},
				"uppercase": true
			}
		];
	
		var config = {
			columnDefs : [
					
					{
						label : 'group',
						model : 'dynamicParameter.group2',
						showTreeRowHeader : false,
						grouping : {
							groupPriority : 0
						},
						template : '<div><div ng-if="!col.grouping || col.grouping.groupPriority === undefined || col.grouping.groupPriority === null || ( row.groupHeader && col.grouping.groupPriority === row.treeLevel )" class="ui-grid-cell-contents" title="TOOLTIP">{{COL_FIELD CUSTOM_FILTERS}}</div></div>'
					},
					{
						label : 'name',
						model : 'dynamicParameter.name'
					}, 
					{
						label : 'value',
						model : 'dynamicParameter.value',
						type: 'dynamic',
						template : '<div fs-grid ng-model=\"row.entity\" fs-grid-config=\"row.entity\" fs-ui-grid=\"grid\" fs-ui-grid-row=\"row\" fs-ui-grid-col=\"col\"></div>'
					}, 
					{
						label : 'dispKey',
						model : 'dynamicParameter.dispKey'
					} ]
	
		};
		$scope.gridOptions = gridService
				.createInnerGridOptions($scope, config);
		$scope.gridOptions.enableRowHeaderSelection = false;
		$scope.gridOptions.data = data;
	
		$scope.expandAll = function() {
			$scope.gridApi.treeBase.expandAllRows();
		};
	
		$scope.toggleRow = function(rowNum) {
			$scope.gridApi.treeBase
					.toggleRowTreeState($scope.gridApi.grid.renderContainers.body.visibleRowCache[rowNum]);
		};
	
		$scope.changeGrouping = function() {
			$scope.gridApi.grouping.clearGrouping();
			$scope.gridApi.grouping.groupColumn('dynamicParameter.dispKey');
			$scope.gridApi.grouping.aggregateColumn('dynamicParameter.dispKey', uiGridGroupingConstants.aggregation.COUNT);
		};
	
		$scope.getAggregates = function() {
			var aggregatesTree = [];
			var recursiveExtract = function(treeChildren) {
				return treeChildren.map(function(node) {
					var newNode = {};
					angular.forEach(node.row.entity, function(attributeCol) {
						if (typeof (attributeCol.groupVal) !== 'undefined') {
							newNode.groupVal = attributeCol.groupVal;
							newNode.aggVal = attributeCol.value;
						}
					});
					newNode.otherAggregations = node.aggregations.map(function(aggregation) {
						return {
							colName : aggregation.col.name,
							value : aggregation.value,
							type : aggregation.type
						};
					});
					if (node.children) {
						newNode.children = recursiveExtract(node.children);
					}
					return newNode;
				});
			}
			aggregatesTree = recursiveExtract($scope.gridApi.grid.treeBase.tree);
			console.log(aggregatesTree);
		};
	} ]);
})(angular);